<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/10/11
 * Time: 11:13
 */
namespace app\models;

use think\Model;

/**
 * Class MaterialModel
 * @package app\models
 */
class MaterialModel extends Model
{
    protected $table = 'mt_material';

    /**
     * 订单列表 分页
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     * @throws \think\exception\DbException
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc')
    {
        if ($where) {
            $result = self::where($where)->order($order)->paginate($limit, false, ['page' => $page]);
        } else {
            $result = self::order($order)->paginate($limit, false, ['page' => $page]);
        }
        if (!$result->total()) {
            return ['code' => 0, 'msg' => "ok", 'count' => 0, 'data' => []];
        }
        $url = config('ypyun')['yourdownloadurl'];
        foreach ($result as $key => $value) {
            $result[$key]['pic'] = '<img src="'.$url.'/'.$value['pic'] . '" style="width:70px;height:40px;">';
            $keywords = explode(',',$result[$key]['keyword']);
            $word = '';
            foreach ($keywords as $val){
                $word .= '【'.$val.'】';
            }
            $result[$key]['keyword'] = $word;
            $result[$key]['cate'] = CateModel::where('id',$value['cate_id'])->value('cate_name');
        }
        $result = $result->toArray();
        return ['code' => 0, 'msg' => "ok", 'count' => $result['total'], 'data' => $result['data']];
    }
}